// pagesMine/pages/articleDetails/articleDetails.js
import ajax from "../../../utils/index"
const app = getApp()
const userInfo = wx.getStorageSync('userInfo')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 2,
    detail: {},
    content: ``,
  },

  /**
     * 接口 - 获取文章详情
     */
  get_artcle_detail: function (id) {
    ajax.get('/wx/AppletArticle/getInfo', { id }).then(res => {
      if (res.code != 200) return;
      let detail = res.data
      detail.content = detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
      this.setData({ detail })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options && options.id) {
      console.log('options', options)
      this.setData({
        type: options.type,
      })
      this.get_artcle_detail(options.id)
      if (options.userid) { //分享打开赋值绑定
        app.globalData.inviterUserId = options.userid
        app.globalData.qrQuery = options
      }
    } else if (app.globalData.qrQuery && app.globalData.qrQuery.id) {
      // console.log( 'globalData', app.globalData.qrQuery)
      this.get_artcle_detail(app.globalData.qrQuery.id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let userid = userInfo.id
    return {
      title: that.data.detail.title,
      path: '/pagesMine/pages/articleDetails/articleDetails?userid=' + userid + '&type=2&id=' + that.data.detail.id,
      imageUrl: that.data.detail.logoImg,
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    let userid = userInfo.id
    console.log(userid)
    return {
      title: that.data.detail.title,
      query: 'userid=' + userid + '&type=2&id=' + that.data.detail.id,
      imageUrl: that.data.detail.logoImg,
    }
  }
})